from etgui import *
from PageLayout import *
# A Dialog edits the values of a PotGuiModel.
# You can set the values with
#   model.Set("SlotName",value)
# and get the values with
#    model.Get("SlotName")
# The GuiElements are bound to the values.
# When the value is changed in the gui, the model
# value is automatically changed.
# And if you change the model value the GUI value
# is changed too.

def MakeView():
	"""Create the GUI elements"""
	# The Popup changes the value "ChoiceValue" in
	# the model
	popup=PotGuiPopupButton("ChoiceValue")
	# AddItem(display_name,value_in_model)
	# the value_in_model could also be an integer
	popup.AddItem("Do This","this")
	popup.AddItem("Do That","that")
	popup.AddItem("Item 3","value3")

	view=MakePageLayout(
		eVObjHExpand|eVObjVExpand, # how to align the items on the page
		Form(
			# the items are pairs of label,gui-element
			items=[
				"Text Label 1", PotTextField("Text1"), # edit "Text1" in model
				"Popup label",  popup,
				"IntField",  PotGuiIntField("N",IntField(-1,2,0,99))
			]
		),
		# the "MultiLineText" in the model is a TextView
		PotGuiEnabler(
		    "N",
		    VBox(
			align=eVObjHExpand|eVObjVExpand,
			items=[
			    TextItem("Bla"),
			    PotGuiTextView("MultiLineText",TextView(),Point(200))
			]
		    )
		),
	)
	return view

def p(model,slot):
	"""Print the value of a model slot"""
	print `slot`,"=",`model.GetValue(slot)`

# important: keep one copy of the dialog
gDialog=None
gModel=None
class MyModel(DictPyModel):
    def DoSetValue(self,path,value):
	print `path`,`value`
	if path=="ChoiceValue":
	    self.SetValue("N",(self.GetValue("N")+1)%2)
	return DictPyModel.DoSetValue(self,path,value)
    def DoIsValueChangable(self,slot):
	return 1
	return DictPyModel.DoIsValueChangable(self,slot)

def Show():
	global gDialog,gModel
	if not gDialog:
		# create the dialog once!
		gModel=MyModel()
		gDialog=MakeOkCancelDialog("My Dialog",gModel,MakeView())
		# set this value only the first time and
		# remember when dialog is opened next time
		gModel.SetValue("Text1","bla")
	# get the dialogs model
	# set the following values each time the dialog is shown
	gModel.SetValue("N",0)
	gModel.SetValue("ChoiceValue","that")
	gModel.SetValue("MultiLineText","my\ntext")

	# show the dialog
	if gDialog.ShowUnderMouse()==CmdNo.cIdOk:
		# The user pressed OK. Do soemthing!
		# Let's print the values of the model slots
		p(gModel,"Text1")
		p(gModel,"IntValue1")
		p(gModel,"OneOfPopup")
		print gModel.dict
if __name__=="__main__":
    Show()

